package com.bw.android.bwgao6week2a.vm;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.bw.android.network.RequestClient;
import com.bw.android.network.bean.LiveListBean;

import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Author: conghao
 * Date: 2023/9/21 10:00
 * Description: LiveListViewModel
 */
public class LiveListViewModel extends ViewModel {
    public MutableLiveData<LiveListBean> liveListData = new MutableLiveData<>();
    public MutableLiveData<Throwable> throwData = new MutableLiveData<>();
    private Disposable mDisposable;

    public void loadData(final int pageNo, final int rows) {
        dispose();
        RequestClient.buildService().getLiveList(0, pageNo, rows)
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(liveListBean -> {
                    liveListData.postValue(liveListBean);
                }, throwable -> {
                    throwData.postValue(throwable);
                });
    }

    private void dispose() {
        if (mDisposable != null) mDisposable.dispose();
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        dispose();
    }
}
